package com.wclass.controller.sys;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.wclass.common.ReturnObject;
import com.wclass.entity.CourseInfo;
import com.wclass.entity.UserWithdrawBill;
import com.wclass.interceptor.IgnoreAuth;
import com.wclass.service.ICourseInfoService;
import com.wclass.service.IUserWithdrawBillService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 打款管理 后台控制器
 * </p>
 *
 * @author Bruce
 * @since 2018-01-016
 */
@RestController
@RequestMapping("/sysMakeMoney")
public class SysMakeMoneyController {
    @Autowired
    private IUserWithdrawBillService iUserWithdrawBillService;

    /**
     *
     * @param limit
     * @param offset
     * @param payState
     * @param cardholder
     * @return
     */
    @ApiOperation("获取我的课程分页分页")
    @IgnoreAuth
    @RequestMapping(value = "/getList", method = RequestMethod.GET)
    public ReturnObject<List<UserWithdrawBill>> getList( @ApiParam("每页条数") @RequestParam(defaultValue = "10") int limit,
                                                      @ApiParam("页数") @RequestParam(defaultValue = "1") int offset,
                                                      @ApiParam("审核状态") Integer payState,
                                                      @ApiParam("提现人名字") String cardholder) {
        try {
            EntityWrapper<UserWithdrawBill> ew = new EntityWrapper<>();
            if(payState!=null){
                ew.eq("pay_state", payState);
            }
            if(StringUtils.isNotBlank(cardholder)){
                ew.like("cardholder", cardholder);
            }
            ew.orderBy("crt_time", false);
            Page<UserWithdrawBill> obj = iUserWithdrawBillService.selectPage(new Page<>(offset, limit), ew);
            return new ReturnObject<>(0, "获取成功", obj.getTotal(), obj.getPages(), obj.getRecords());

        } catch (Exception e) {
            e.printStackTrace();
            return new ReturnObject<>(12, "系统偷懒了");
        }
    }

    /**
     *  打款
     * @param id
     * @param payAmount
     * @return
     */
    @ApiOperation("打款")
    @IgnoreAuth
    @RequestMapping(value = "/payMoney", method = RequestMethod.GET)
    public ReturnObject  payMoney(@ApiParam("id") Long id,@ApiParam("实际打款金额") Long payAmount) {
        try {
            UserWithdrawBill uwb=iUserWithdrawBillService.selectById(id);
            if(uwb!=null){
                uwb.setPayAmount(payAmount*100);
                uwb.setPayState(1);
                iUserWithdrawBillService.updateById(uwb);
                return new ReturnObject<>(0, "打款成功！");
            }else{
                return new ReturnObject<>(0, "打款失败！");
            }

        } catch (Exception e) {
            e.printStackTrace();
            return new ReturnObject<>(12, "系统偷懒了");
        }
    }
}

